package com.power200.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.power200.android.R;
import com.power200.android.bean.BackLog;
import com.power200.android.enumeration.BacklogEnum;
import com.power200.android.service.BackLogService;

import java.util.List;

public class BacklogFragment extends Fragment {


    public static final int MENU_DELETE = 1;
    public static final int MENU_W0RKING = 2;
    public static final int MENU_COMPLETE = 3;
    public static final int MENU_HOLD = 4;

    private boolean available = true;
    private List<BackLog> backLogList;
    private BackLogService backLogService = BackLogService.getInstance();

    private ListView backlogListView;

    private void refreshData() {
        if(available) {
            backLogList = backLogService.getAvailableBackLogList();
        } else {
            backLogList = backLogService.getBackLogs();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_backlog, container, false);
        processCreateButton(view);
        processBacklogListView(view, inflater);
        return view;
    }

    private void processCreateButton(View view) {
        ImageButton createButton = (ImageButton) view.findViewById(R.id.createBacklogButton);
        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BacklogCreate.class);
                startActivity(intent);
            }
        });

        CheckBox checkBox = (CheckBox) view.findViewById(R.id.hideCheckBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                available = isChecked;
                Log.i("CheckBox",""+available);
                refreshData();
                Log.i("CheckBox", "size:" + backLogList.size());

                ListAdapter adapter = backlogListView.getAdapter();

                if (adapter instanceof HeaderViewListAdapter) {
                    adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
                }

                if (adapter instanceof BacklogAdapter) {
                    ((BacklogAdapter) adapter).notifyDataSetChanged();
                    ((BacklogAdapter) adapter).setData(backLogList);
                }
            }
        });
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = menuInfo.position - backlogListView.getHeaderViewsCount();
        final BackLog backLog = backLogList.get(position);
        ListAdapter adapter = backlogListView.getAdapter();

        switch (item.getItemId()) {
            case MENU_DELETE:
                Toast.makeText(getActivity(), "" + backLog.getName(), Toast.LENGTH_SHORT).show();
                backLogService.delete(backLog);
                break;
            case MENU_COMPLETE:
                backLog.setStatus(BacklogEnum.COMPLETE.value());
                backLogService.refresh();
                break;
            case MENU_W0RKING:
                backLog.setStatus(BacklogEnum.WORKING.value());
                backLogService.refresh();
            case MENU_HOLD:
                backLog.setStatus(BacklogEnum.HOLD.value());
                backLogService.refresh();
            default:
                break;
        }
        if (adapter instanceof HeaderViewListAdapter) {
            adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
        }

        if (adapter instanceof BacklogAdapter) {
            ((BacklogAdapter) adapter).notifyDataSetChanged();
        }
        return super.onContextItemSelected(item);
    }

    private void processBacklogListView(View view, LayoutInflater inflater) {
        backlogListView = (ListView) view.findViewById(R.id.backlog);
        refreshData();
        Log.i("Hell", "Bak");
        backlogListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Please select operations:");
                menu.add(0, MENU_DELETE, 0, "Delete");
                menu.add(0, MENU_W0RKING, 1, "Working");
                menu.add(0, MENU_COMPLETE, 2, "Complete");
                menu.add(0, MENU_HOLD, 3, "Hold");
            }
        });
        backlogListView.setAdapter(new BacklogAdapter(inflater, backLogList));
    }

    private View header() {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setOrientation(LinearLayout.HORIZONTAL);

        TextView priority = new TextView(getActivity());
        priority.setText("Priority");
        priority.setWidth(80);
        layout.addView(priority);

        TextView name = new TextView(getActivity());
        name.setText("Category");
        name.setWidth(80);
        layout.addView(name);

        TextView brief = new TextView(getActivity());
        brief.setText("Brief");
        layout.addView(brief);

        return layout;
    }

    public final class ViewHolder {
        protected TextView priorityText;
        protected TextView nameText;
        protected TextView briefText;
        protected TextView descriptionText;
        protected TextView statusText;
        protected Button editButton;
    }


    public class BacklogAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;
        private List<BackLog> backLogs;
        private View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                v.get
            }
        };

        public void setData(List<BackLog> backLogs) {
            this.backLogs = backLogs;
        }

        public BacklogAdapter(LayoutInflater inflater, List<BackLog> backLogs) {
            this.layoutInflater = inflater;
            this.backLogs = backLogs;
        }

        @Override
        public int getCount() {
            return backLogs.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // position 是界面的position,不是data的position
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.backlog_item, null);
                holder.priorityText = (TextView) convertView.findViewById(R.id.backlog_item_priority);
                holder.nameText = (TextView) convertView.findViewById(R.id.backlog_item_name);
                holder.briefText = (TextView) convertView.findViewById(R.id.backlog_item_brief);
                holder.descriptionText = (TextView) convertView.findViewById(R.id.backlog_item_description);
                holder.statusText = (TextView) convertView.findViewById(R.id.backlog_item_status);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.priorityText.setText("" + backLogs.get(position).getPriority());
            holder.nameText.setText(backLogs.get(position).getName());
            holder.briefText.setText(backLogs.get(position).getBrief());
            holder.descriptionText.setText(backLogs.get(position).getDescription());

            int status = backLogs.get(position).getStatus();
            holder.statusText.setText(BacklogEnum.display(status));
            return convertView;
        }
    }

}
